package org.pageley.games.sanctuary.domain;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;


public class SimpleLocation implements Location {

	private int x;
	private int y;
	
	public SimpleLocation(int x, int y) {
		this.x = x;
		this.y = y;
	}

	@Override
	public boolean equals(Object obj) {
		boolean result = false;
		if(obj instanceof SimpleLocation) {
			SimpleLocation other = (SimpleLocation) obj;
			result = new EqualsBuilder().append(this.x, other.x).append(this.y, other.y).isEquals();
		}
		return result;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.x).append(this.y).toHashCode();
	}

	@Override
	public String toString() {
		return String.format("[%1$s, %2$s]", this.x, this.y);
	}

	public Location locationIn(Direction direction) {
		return direction.fromLocation(this);
	}

	public int getX() {
		return this.x;
	}

	public int getY() {
		return this.y;
	}

	@Override
	public double distanceTo(Location location) {
		return Math.sqrt(Math.pow(x - location.getX(), 2) + Math.pow(y - location.getY(), 2));
	}

	@Override
	public int squaresTo(Location location) {
		return Math.max(
				Math.abs(x - location.getX()), 
				Math.abs(y - location.getY()));
	}
	
}
